package mashuaiproject.mashuaiproject.serivce;


import mashuaiproject.mashuaiproject.dto.*;
import mashuaiproject.mashuaiproject.mapper.*;
import mashuaiproject.mashuaiproject.model.CommentSon;
import mashuaiproject.mashuaiproject.model.Comments;
import mashuaiproject.mashuaiproject.model.Quesions;
import mashuaiproject.mashuaiproject.model.Users;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service//这个类就是把连个表进行管理使用的类
public class QuesionUserSerivce {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private QuesionMapper quesionMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentSonMapper commentSonMapper;

    @Autowired
    private QuesionUserSerivce quesionUserSerivce;
    @Autowired
    private AttributeMappper attributeMappper;

    public PaginationDto selectQ_UList(Integer page, Integer size,String serachQuesions,String sorts) {
        //分页
        System.err.println("serachQuesions:"+serachQuesions+"-sorts:"+sorts+"-page:"+page+"-size:"+size);
        Integer totolCount=quesionMapper.count();

        //根据问题总数自动变化size
        if(totolCount>=15 && totolCount<=20){
            size=10;
        }else if(totolCount>20){
            size=15;
        }


        Integer totolPage=0;//默认总页数是0
        if(totolCount%size==0){
            totolPage=totolCount/size;
        }else{
            totolPage=totolCount/size+1;
        }
        if(page<=0){
            page=1;
        }
        if(page>totolPage){
            page=totolPage;
        }
        Integer offset=size*(page-1);//需要从第几条显示的数据

        //判断文章是不是查找的
        List<Quesions> quesionsList=new ArrayList<>();
        if(serachQuesions!=null){
            quesionsList=quesionMapper.selectByTitle(serachQuesions);
        }else{
          //根据每个文章去查找对应的用户信息，根据quesions表的creator 和users的id进行关联
                quesionsList=quesionMapper.selectAllQeusions(offset,size);
        }


        if(sorts!=null){
            if(sorts.equals("new")){
                quesionsList=quesionMapper.selectNewQuesion(offset,size);
            }
           if(sorts.equals("hot")){
                quesionsList=quesionMapper.selectHotQuesion(offset,size);//根据点击字数最多的计算
            }
            if(sorts.equals("hot1")){
                //根据定时器，每其他请求一次
                quesionsList=quesionMapper.selectNewQuesion(offset,size);
            }
            if(sorts.equals("hot7")){
                System.out.println("sort77:");
                quesionsList=quesionMapper.selectNewQuesion(offset,size);
            }
            if(sorts.equals("no")){

                quesionsList=quesionMapper.selectNoCommentQuesion(offset,size);
            }
        }


        List<QuesionUserDto> quesionUserDtos=new ArrayList<>();
        for(Quesions quesions:quesionsList){
            //取出来每个creatorj进行查询
            Users users=userMapper.findByUser(quesions.getCreator());
            //将信息复给每个quesionUserDto对象sa完成两个表之间的关联
            QuesionUserDto quesionUserDto=new QuesionUserDto();
            BeanUtils.copyProperties(quesions,quesionUserDto);
            quesionUserDto.setUsers(users);

            quesionUserDtos.add(quesionUserDto);
        }

        PaginationDto paginationDto = new PaginationDto();//将当前关联好的数据在封装到PaginationDto类到对象里面。里面包含分页的信息
        paginationDto.setQuesionUserDtos(quesionUserDtos);


        paginationDto.setIsSerach(serachQuesions);
        //将页数传递进去

        System.err.println("接受到值"+totolCount+size+page);
        paginationDto.setPagination(totolCount,size,page);

        return paginationDto;

    }

    public PaginationDto selectQ_UList(Integer id, Integer page, Integer size) {
        //分页
        Integer totolCount=quesionMapper.count2(id);
        Integer totolPage=0;//默认总页数是0
        if(totolCount%size==0){
            totolPage=totolCount/size;
        }else{
            totolPage=totolCount/size+1;
        }
        if(page<=0){
            page=1;
        }
        if(page>totolPage){
            page=totolPage;
        }
        Integer offset=size*(page-1);//需要从第几条显示的数据
        //根据每个文章去查找对应的用户信息，根据quesions表的creator 和users的id进行关联
        if(totolCount==0){
            id=666;
            offset=0;
            size=1;
        }
        List<Quesions> quesionsList=quesionMapper.selectAllQeusions2(id,offset,size);
        List<QuesionUserDto> quesionUserDtos=new ArrayList<>();
        for(Quesions quesions:quesionsList){
            //取出来每个creatorj进行查询
            Users users=userMapper.findByUser(quesions.getCreator());
            //将信息复给每个quesionUserDto对象sa完成两个表之间的关联
            QuesionUserDto quesionUserDto=new QuesionUserDto();
            BeanUtils.copyProperties(quesions,quesionUserDto);
            quesionUserDto.setUsers(users);
            quesionUserDtos.add(quesionUserDto);
        }
        PaginationDto paginationDto = new PaginationDto();//将当前关联好的数据在封装到PaginationDto类到对象里面。里面包含分页的信息
        paginationDto.setQuesionUserDtos(quesionUserDtos);
        //将页数传递进去

        System.err.println("接受到值"+totolCount+size+page);
        paginationDto.setPagination(totolCount,size,page);

        return paginationDto;
    }

    public QuesionUserDto getByidQuesionUser(Integer id) {
        System.out.println("文章的id");
        //首先根据文章的id确定文章
        Quesions quesionsById=quesionMapper.getByidQuesion(id);
        //在根据文章的creator确定当前用户
        Users users=userMapper.findByUser(quesionsById.getCreator());
        QuesionUserDto quesionUserDto=new QuesionUserDto();
        BeanUtils.copyProperties(quesionsById,quesionUserDto);
        quesionUserDto.setUsers(users);
        //System.err.println(quesionUserDto.toString());
        return quesionUserDto;
    }
//添加阅读数
    public int addViewConut(Integer id) {
        int updateViewConut=quesionMapper.UpdateViewCount(id);
        return updateViewConut;
    }

//查看所有的父评论
    public List<CommentListDto> ListBuyQusionId(Integer id,Integer type) {
        //首先根据文章的id和comments的表parent_id关联·1，查找出当前的评论
        List<CommentListDto> commentListDtoList=new ArrayList<>();
        //一级评论
        if (type==1){
            List<Comments> commentsList=commentMapper.selectListCommentType_One(id);//查找丑所有关于这个文章的一级评论
            //在根据每个评论人的commentotr查找对应的用户信息
            for(Comments commentsList2:commentsList){
                int commentSonCount=commentMapper.getCommentSonConut(commentsList2.getId());//二级评论数量
                int fabulousUpConut=commentMapper.getFabulousUpConut(commentsList2.getId());//点赞上升数量
                int fabulouDownConut=commentMapper.getFabulousDownConut(commentsList2.getId());//点赞下将的数量
                CommentListDto commentListDtos=new CommentListDto();
                //System.err.println(commentsList2.toString());
                Users users=userMapper.findByUser(commentsList2.getCommentator());
                commentListDtos.setUsers(users);
                commentListDtos.setCommentson_count(commentSonCount);//二级评论总数
                commentListDtos.setFabulous_down(fabulouDownConut);
                commentListDtos.setFabulous_up(fabulousUpConut);
                BeanUtils.copyProperties(commentsList2,commentListDtos);
                commentListDtos.setGmt_create(commentsList2.getGmt_create());
                commentListDtoList.add(commentListDtos);
                //将对应的用户和信息进行绑定
            }
        }

        return commentListDtoList;
    }
    public List<CommentsSonDto> ListBuyIdCommentSon(Integer fatherId, List<CommentSon> commentSonList) {
        List<CommentsSonDto> commentsSonDtoList=new ArrayList<>();
        //获取当前用户
        //根据父评论的id找到对应的文章
        List<CommentSon> commentSons=commentSonMapper.selectAllCommentSon(fatherId);
        System.err.println("传递过来的父类评论："+fatherId+"--查找到的二级评论数："+commentSons.size());
        if(commentSons.size()!=0){

            for (CommentSon commentSon:commentSons){
                //查找对应的user对象
                Users users=userMapper.findByUser(commentSon.getUser_id());
                CommentsSonDto commentsSonDto=new CommentsSonDto();
                BeanUtils.copyProperties(commentSon,commentsSonDto);
                commentsSonDto.setUsers(users);
                commentsSonDtoList.add(commentsSonDto);

            }
            return commentsSonDtoList;
        }
        System.out.println("查找的为空");
        return commentsSonDtoList;
    }

    public List<Quesions> getByConnectQuesions(Integer id) {
        //g根据id获取文章的标签Q   java|springboot,springmvc,c++,c#
       String queisoTags=quesionMapper.selectQusionTags(id);
        String connect_tages="";
        //这里还要判断下输入的标签是不是包含++登特殊符号
        if(queisoTags.contains(",")){
            String tags[]= queisoTags.split(",");
            String regexpTag= Arrays.stream(tags).collect(Collectors.joining("|"));
            connect_tages=regexpTag;
        }else{
            connect_tages=queisoTags;
        }
        System.out.println("connect_tages:"+connect_tages);
        //查找出相关的所有问题，并且值选取前十个
        List<Quesions> connect_quesions=quesionMapper.selctConnetTagQusion(connect_tages,id);
        System.out.println("相关文章的条数："+connect_quesions.size());
        //根据这个id
        return  connect_quesions;
    }

    public List<CommentSon> selectAllCommentSonById(Integer fatherId) {
        List<CommentSon> commentSonList=quesionMapper.selectAllCommentSonByIds(fatherId);
        return  commentSonList;
    }

    public List<Quesions> hotQuesions() {
        List<Quesions> hotQuesions=quesionMapper.selectHotQueisons();
        return  hotQuesions;
    }

    public Integer selectCreator(Integer queiosnIs) {
        Integer crator=quesionMapper.slectCreateor(queiosnIs);
        return crator;
    }

    public Users slectUser(Integer crator) {
        Users slectUser=userMapper.findByUser(crator);
        return  slectUser;

    }

    public int delQueiosnId(Integer id, Integer quesionId) {
        //删除文章，首先需要删除与这个文章的所有关联的
        //首先是

        return 1;
    }

    public List<CommentListDto> selectOneComment(Integer id,Integer page,Integer size,String sort) {
        //查找出当前用户的总收藏信息
        Integer countPage=0;//总页数
        Integer offset=(page-1)*size;//第几条显示的条数
        Integer totulout=commentMapper.getCommentOneComment(id);
        if(totulout%size==0){
            countPage=totulout/size;
        }else if(totulout%size!=0){
            countPage=totulout/size+1;
        }

        List<Comments> comments=new ArrayList<>();
        //这里进行排序判断
        if(!sort.equals("no")){
            if(sort.equals("timeUp")){
                comments=commentMapper.selectOneCommentstimeUp(id,offset,size);

            }
            if(sort.equals("timeDown")){
                comments=commentMapper.selectOneCommentstimeDown(id,offset,size);

            }
            if(sort.equals("fabulousUp")){
                comments=commentMapper.selectOneCommentstimefabulousUp(id,offset,size);
            }
            if(sort.equals("fabulousDown")){
                comments=commentMapper.selectOneCommentstimefabulousDown(id,offset,size);
            }
        /*    if(sort.equals("hotUp")){

            }
            if(sort.equals("hotDown")){

            }*/
        }else {
           comments=commentMapper.selectOneComments(id,offset,size);
        }
        //查找出该用户所有的评论
        List<CommentListDto> commentListDtos=new ArrayList<>();
        for(Comments comments1:comments){
            //查处文章对应的博主
            Integer bozhu=quesionMapper.slectCreateor(comments1.getParent_id());//
            Users users=userMapper.findByUser(bozhu);
            Quesions quesions=quesionMapper.selectQueiosns(comments1.getParent_id());
           CommentListDto commentListDto=new CommentListDto();
           BeanUtils.copyProperties(comments1,commentListDto);
           commentListDto.setQuesions(quesions);
           commentListDto.setUsers(users);
           commentListDto.setPage(page);
           commentListDto.setCountPage(countPage);
           //判断当前用户是不是被关注过了
            //显示改用户是否关注这个帖子
            Integer bozhu_id=quesionUserSerivce.selectCreator(quesions.getId());
            Integer checkGuanzhu=attributeMappper.checkGuanZhu(bozhu_id,id);
            //找出当前博主的粉丝
            Integer fensiCount=attributeMappper.selectFenSiCount(bozhu_id);
            if (checkGuanzhu!=0){
                //已经关注
                commentListDto.setIsGuanzhu("yes");
            }else{
                commentListDto.setIsGuanzhu("no");
            }


           commentListDtos.add(commentListDto);

        }
      // System.err.println("长度："+commentListDtos.get(0));
        return commentListDtos;
    }
}
